package com.tengju.bff.interfaces.app.charge;

import com.idanchuang.income.ServiceIncome;
import com.tengju.bff.interfaces.app.charge.facade.ChargeFacade;
import com.tengju.bff.interfaces.app.charge.vo.MonthChargeChartVO;
import com.tengju.bff.interfaces.app.charge.vo.MonthChargeDetailVO;
import com.tengju.bff.interfaces.app.charge.vo.MonthChargeListVO;
import com.tengju.bff.interfaces.shared.ApiResponse;
import com.tengju.bff.interfaces.shared.InterfaceException;
import com.tengju.bff.interfaces.shared.SystemException;
import com.tengju.bff.interfaces.shared.servlet.ServletContextUtil;
import com.tengju.bff.interfaces.shared.servlet.UserServletContext;
import com.tengju.user.domain.model.user.UserIdCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

import static com.tengju.user.domain.shared.ResultCodeEnum.SYS_EXCEPTION;

@RestController
@Api(tags = "DT App服务费明细接口")
@RequestMapping(value = "charge")
public class ChargeController {


    private final ChargeFacade chargeFacade;

    @Autowired
    public ChargeController(ChargeFacade chargeFacade, ServiceIncome serviceIncome) throws Exception {
        this.chargeFacade = chargeFacade;
        serviceIncome.createBuilder();
    }


    @ApiOperation(value = "查询服务费曲线接口", notes = "查询服务费曲线接口")
    @GetMapping(value = "line", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ApiResponse<MonthChargeChartVO> line(String time) {
        try {
            UserServletContext servletContext = ServletContextUtil.getServletContext();
            UserIdCode userIdCode = servletContext.needIdCode();
            return ApiResponse.newSuccess(chargeFacade.castLine(userIdCode, time));
        } catch (SystemException e) {
            return ApiResponse.newFailure(e.getErrorCode(), e.getErrorMessage());
        } catch (Exception e) {
            return ApiResponse.newFailure(SYS_EXCEPTION.getCode(), e.getMessage());
        }
    }


    @ApiOperation(value = "广告指数列表数据", notes = "广告指数列表数据")
    @GetMapping(value = "list", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ApiResponse<List<MonthChargeListVO>> list(String time) {
        try {
            UserServletContext servletContext = ServletContextUtil.getServletContext();
            UserIdCode userIdCode = servletContext.needIdCode();


            return ApiResponse.newSuccess(chargeFacade.castList(userIdCode, time));
        } catch (InterfaceException e) {
            return ApiResponse.newFailure(e.getCode(), e.getMsg());
        }
    }


    @ApiOperation(value = "查询月度服务费明细接口", notes = "查询服务费明细接口")
    @GetMapping(value = "month", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ApiResponse<List<MonthChargeDetailVO>> mouth(String date, String amount) {
        try {

            UserServletContext servletContext = ServletContextUtil.getServletContext();

            UserIdCode idCode = servletContext.needIdCode();

            return ApiResponse.newSuccess(chargeFacade.castMonth(idCode, date, amount));
        } catch (InterfaceException e) {
            return ApiResponse.newFailure(e.getCode(), e.getMsg());
        }
    }

}
